﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;

namespace Delay
{

    public static class SuperUrlConver
    {
        /// <summary>
        /// Gets the value of the Uri to use for providing the contents of the Image's Source property.
        /// </summary>
        /// <param name="obj">Image needing its Source property set.</param>
        /// <returns>Uri to use for providing the contents of the Source property.</returns>
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "TextSource is applicable only to TextBlock elements.")]
        public static string GetTextSource(DependencyObject obj)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }
            return (string)obj.GetValue(TextSourceProperty);
        }
        /// <summary>
        /// Sets the value of the Uri to use for providing the contents of the Image's Source property.
        /// </summary>
        /// <param name="obj">Image needing its Source property set.</param>
        /// <param name="value">Uri to use for providing the contents of the Source property.</param>
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "TextSource is applicable only to TextBlock elements.")]
        public static void SetTextSource(DependencyObject obj, Uri value)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }
            obj.SetValue(TextSourceProperty, value);
        }
        public static readonly DependencyProperty TextSourceProperty = DependencyProperty.RegisterAttached(
           "TextSource", typeof(string), typeof(SuperUrlConver), new PropertyMetadata(OnTextSourceChanged));
        //
        private static void OnTextSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            RichTextBox resolving = (RichTextBox)o;
            if (resolving == null)
            {
                return;
            }
            string content = (string)e.NewValue;
            int start = content.IndexOf("<a");
            Paragraph parag = new Paragraph();
            Color color = Color.FromArgb(0xFF, 0x99, 0x99, 0x99);
           
            if (start<0)
            {
                
                Run run = new Run();
                run.Text = content;
                run.FontSize = 22;
                run.Foreground = new SolidColorBrush(color);
                parag.Inlines.Add(run);
                resolving.Blocks.Clear();
                resolving.Blocks.Add(parag);
               
            
                return;
            }
            int end = content.LastIndexOf("a>");
            if (end<0)
            {
               
                Run run = new Run();
               
                run.Foreground = new SolidColorBrush(color);
                run.FontSize = 22;
                run.Text = content;
                parag.Inlines.Add(run);
                resolving.Blocks.Clear();
                resolving.Blocks.Add(parag);
                return;
            }
          
            
         
            //

            Run startrun = new Run();
            startrun.Foreground = new SolidColorBrush(color);
            startrun.FontSize = 22;
            startrun.Text = content.Substring(0, start);
            parag.Inlines.Add(startrun);
            parsSuperUrl(content.Substring(start, end + 2 - start), resolving, parag);
           
            Run endrun = new Run();
            endrun.Foreground = new SolidColorBrush(color);
            endrun.FontSize = 22;
            endrun.Text = content.Substring(end + 2, content.Length - (end + 2));
            parag.Inlines.Add(endrun);
            resolving.Blocks.Clear();
            resolving.Blocks.Add(parag);
           
        }
        [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "Static constructor performs additional tasks.")]
        static SuperUrlConver()
        {
           // Start worker thread            
           // Application.Current.Exit += new EventHandler(HandleApplicationExit);           
        }
        private static void parsSuperUrl(string url, RichTextBox resolving, Paragraph parag) 
        {
           
            int start=url.IndexOf("<a");
            Color color = Color.FromArgb(0xFF, 0x99, 0x99, 0x99);
            while (start>=0)
            {
                int attributestart = url.IndexOf("href=", start);
                int attributeend = url.IndexOf(">", start);
                int keyend = url.IndexOf("</a>", start);

                if (attributestart < 0 || attributeend < 0 || keyend<0)
                {
                    
                }
                string str_attribute = url.Substring(attributestart + 6, attributeend - attributestart - 7).Trim();
               string str_value = url.Substring(attributeend + 1, keyend - attributeend - 1).Trim();
               string pageName = "/Page/LexunMap.xaml";
               pageName += "?url=" + str_attribute;
               Uri uri = new Uri(pageName, UriKind.Relative);
               Hyperlink temprun = new Hyperlink();
               Run run=new Run(); 
               run.Text = str_value;
               temprun.Inlines.Add(run);
               temprun.NavigateUri = uri;
               //temprun.TargetName = "_blank";

               parag.Inlines.Add(temprun);
               int  start1 = url.IndexOf("<a", keyend);
               if (start1 < 0)
               {
                   start = start1;
               }
               else
               {
                   Run endrun = new Run();
                   endrun.Foreground = new SolidColorBrush(color);
                   endrun.FontSize = 22;
                   endrun.Text =(url.Substring(keyend+4, start1 - keyend-4)).Trim();
                   parag.Inlines.Add(endrun);
                   start = start1;
               }
            }
            //return dic;
        }
    }
}
